package com.hxkcloud.nurse.utils;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


import com.hxkcloud.nurse.config.ApplicationConfig;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * 图片压缩
 * @author MingNuo
 *
 */
public class MyImageCompresseUtils
{
    private static String TAG = "compresse";

    public static JSONArray processPics(final ArrayList<String> pics)
    {
        Log.i(TAG,"pics:"+pics);
        JSONArray localUrls = null;
        try
        {
            localUrls = new JSONArray();
            Log.i(TAG, "压缩前的时间----" + System.currentTimeMillis());
            for (int i = 0; i < pics.size(); i++)
            {
                Bitmap bit = compressImageFromFile(pics.get(i));
                Log.d(TAG, "pics:" + pics.get(i));
                String fileName = System.currentTimeMillis() + ".png";
                File filePath = new File(ApplicationConfig.PicAndVideoUrl);
                if (!filePath.exists())
                {
                    filePath.mkdirs();
                }
                File file = new File(ApplicationConfig.PicAndVideoUrl, fileName);
                compressBmpToFile(bit, file);

                JSONObject urljson = new JSONObject();
                urljson.put("url",ApplicationConfig.PicAndVideoUrl + File.separator + fileName);
                urljson.put("size",getFileSize(file.length()));
                localUrls.put(urljson);
//				localUrls.put(Constant.IMAGE_DIRECTORY + File.separator + fileName);
            }
//            localUrls
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

        return localUrls;
    }

    @SuppressWarnings("deprecation")
    public static Bitmap compressImageFromFile(String srcPath)
    {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;// 只读边,不读内容
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = 800f;//
        float ww = 480f;//
        int be = 1;
        if (w > h && w > ww)
        {
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh)
        {
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;// 设置采样率

        newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;// 该模式是默认的,可不设
        newOpts.inPurgeable = true;// 同时设置才会有效
        newOpts.inInputShareable = true;// 。当系统内存不够时候图片自动被回收

        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return bitmap;
    }

    public static void compressBmpToFile(Bitmap bmp, File file)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int options = 80;

        bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
        while (baos.toByteArray().length / 1024 > 80)
        {
            baos.reset();
            options -= 10;
            bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
        }
        try
        {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static String getFileSize(long length)
    {
        DecimalFormat df = new DecimalFormat("#.0");
        return df.format(length*1.0/1024/1024);
    }


    /**
     * 将本地图片转换成bitmap
     * @param pathString
     * @return
     */
    public static Bitmap getDiskBitmap(String pathString)
    {
        Bitmap bitmap = null;
        try
        {
            File file = new File(pathString);
            if(file.exists())
            {
                bitmap = BitmapFactory.decodeFile(pathString);
            }
        } catch (Exception e)
        {
            // TODO: handle exception
        }
        return bitmap;
    }
}
